﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class HeSoBO : Csla.BusinessBase<HeSoBO>
    {
        #region Business Properties

        private int? _HeSoID = null;
        public int? CL_HeSoID
        {
            get { return this._HeSoID; }
            set { this._HeSoID = value; PropertyHasChanged("CL_HeSoID"); }
        }

        private string _LoaiHeSo = null;
        public string CL_LoaiHeSo
        {
            get { return this._LoaiHeSo; }
            set { this._LoaiHeSo = value; PropertyHasChanged("CL_LoaiHeSo"); }
        }

        private int? _SoThuTu = null;
        public int? CL_SoThuTu
        {
            get { return this._SoThuTu; }
            set { this._SoThuTu = value; PropertyHasChanged("CL_SoThuTu"); }
        }

        private string _MaHeSo = null;
        public string CL_MaHeSo
        {
            get { return this._MaHeSo; }
            set { this._MaHeSo = value; PropertyHasChanged("CL_MaHeSo"); }
        }

        private string _NoiDung = null;
        public string CL_NoiDung
        {
            get { return this._NoiDung; }
            set { this._NoiDung = value; PropertyHasChanged("CL_NoiDung"); }
        }

        private double? _GiaTri = 1;
        public double? CL_GiaTri
        {
            get { return this._GiaTri; }
            set { this._GiaTri = value; PropertyHasChanged("CL_GiaTri"); }
        }

        private string _DanhDau = null;
        public string CL_DanhDau
        {
            get { return this._DanhDau; }
            set { this._DanhDau = value; PropertyHasChanged("CL_DanhDau"); }
        }

        private string _GhiChu = null;
        public string CL_GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("CL_GhiChu"); }
        }

        private bool? _MacDinh = false;
        public bool? CL_MacDinh
        {
            get { return this._MacDinh; }
            set { this._MacDinh = value; PropertyHasChanged("CL_MacDinh"); }
        }

        private int? _USRC = Session.LoginID;
        public int? CL_USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("CL_USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? CL_DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("CL_DATC"); }
        }

        private int? _USRM = null;
        public int? CL_USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("CL_USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? CL_DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("CL_DATM"); }
        }

        private bool? _DELE = false;
        public bool? CL_DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("CL_DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _HeSoID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newHeSoID = -1;
        private HeSoBO()
        {
            _HeSoID = newHeSoID--;
        }

        public HeSoBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public HeSoBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static HeSoBO NewRootHeSoBO()
        {
            return DataPortal.Create<HeSoBO>(new RootCriteria());
        }

        internal static HeSoBO NewChildHeSoBO()
        {
            return DataPortal.Create<HeSoBO>(new ChildCriteria());
        }

        public static HeSoBO GetRootHeSoBO(int _HeSoID)
        {
            return DataPortal.Fetch<HeSoBO>(new RootCriteria(_HeSoID));
        }

        public static HeSoBO GetRootHeSoBOByWhere(string where)
        {
            return DataPortal.Fetch<HeSoBO>(where);
        }

        internal static HeSoBO GetChildHeSoBO(SqlDataReader dataReader)
        {
            return new HeSoBO(dataReader);
        }

        public static void DeleteHeSoBO(int _HeSoID)
        {
            DataPortal.Delete(new RootCriteria(_HeSoID));
        }

        public static void DeleteHeSoBO(int _HeSoID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_HeSoID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int HeSoID;
            public bool directDelete = false;
            public RootCriteria(int _HeSoID)
            {
                this.HeSoID = _HeSoID;
                this.directDelete = false;
            }

            public RootCriteria(int _HeSoID, bool directDelete)
            {
                this.HeSoID = _HeSoID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int HeSoID;
            public bool directDelete = false;
            public ChildCriteria(int _HeSoID)
            {
                this.HeSoID = _HeSoID;
                this.directDelete = false;
            }

            public ChildCriteria(int _HeSoID, bool directDelete)
            {
                this.HeSoID = _HeSoID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HeSoBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[HeSoID] = " + criteria.HeSoID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HeSoBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["HeSoID"] is DBNull))
                this._HeSoID = (int)dataReader["HeSoID"];
            if (!(dataReader["LoaiHeSo"] is DBNull))
                this._LoaiHeSo = (string)dataReader["LoaiHeSo"];
            if (!(dataReader["SoThuTu"] is DBNull))
                this._SoThuTu = (int)dataReader["SoThuTu"];
            if (!(dataReader["MaHeSo"] is DBNull))
                this._MaHeSo = (string)dataReader["MaHeSo"];
            if (!(dataReader["NoiDung"] is DBNull))
                this._NoiDung = (string)dataReader["NoiDung"];
            if (!(dataReader["GiaTri"] is DBNull))
                this._GiaTri = (double)dataReader["GiaTri"];
            if (!(dataReader["DanhDau"] is DBNull))
                this._DanhDau = (string)dataReader["DanhDau"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["MacDinh"] is DBNull))
                this._MacDinh = (bool)dataReader["MacDinh"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HeSoBO_InsertBO");
            db.AddInParameter(cmd, "@LoaiHeSo", SqlDbType.NVarChar, this._LoaiHeSo);
            db.AddInParameter(cmd, "@SoThuTu", SqlDbType.Int, this._SoThuTu);
            db.AddInParameter(cmd, "@MaHeSo", SqlDbType.NVarChar, this._MaHeSo);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GiaTri", SqlDbType.Float, this._GiaTri);
            db.AddInParameter(cmd, "@DanhDau", SqlDbType.NVarChar, this._DanhDau);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@MacDinh", SqlDbType.Bit, this._MacDinh);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewHeSoID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._HeSoID = (int)cmd.Parameters["@NewHeSoID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HeSoBO_UpdateBO");
            db.AddInParameter(cmd, "@HeSoID", SqlDbType.Int, this._HeSoID);
            db.AddInParameter(cmd, "@LoaiHeSo", SqlDbType.NVarChar, this._LoaiHeSo);
            db.AddInParameter(cmd, "@SoThuTu", SqlDbType.Int, this._SoThuTu);
            db.AddInParameter(cmd, "@MaHeSo", SqlDbType.NVarChar, this._MaHeSo);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GiaTri", SqlDbType.Float, this._GiaTri);
            db.AddInParameter(cmd, "@DanhDau", SqlDbType.NVarChar, this._DanhDau);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@MacDinh", SqlDbType.Bit, this._MacDinh);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_HeSoID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HeSoBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@HeSoID", SqlDbType.Int, criteria.HeSoID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_HeSoID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
